MultiWayAnovaDialog <- function(){
	initializeDialog(title=gettextRcmdr("Multi-Way Analysis of Variance"))
	UpdateModelNumber()
	modelName <- tclVar(paste("AnovaModel.", getRcmdr("modelNumber"), sep=""))
	modelFrame <- tkframe(top)
	model <- ttkentry(modelFrame, width="20", textvariable=modelName)
	groupBox <- variableListBox(top, Factors(), selectmode="multiple", title=gettextRcmdr("Factors (pick one or more)"))
	responseBox <- variableListBox(top, Numeric(), title=gettextRcmdr("Response Variable (pick one)"))
	optionsFrame <- tkframe(top)
	pairwiseVariable <- tclVar("0")
	pairwiseCheckBox <- tkcheckbutton(optionsFrame, variable=pairwiseVariable)
	onOK <- function(){
		modelValue <- trim.blanks(tclvalue(modelName))
		if (!is.valid.name(modelValue)){
			UpdateModelNumber(-1)
			errorCondition(recall=multiWayAnova, message=sprintf(gettextRcmdr('"%s" is not a valid name.'), modelValue))
			return()
		}
		if (is.element(modelValue, listAOVModels())) {
			if ("no" == tclvalue(checkReplace(modelValue, type=gettextRcmdr("Model")))){
				UpdateModelNumber(-1)
				tkdestroy(top)
				multiWayAnova()
				return()
			}
		}
		groups <- getSelection(groupBox)
		response <- getSelection(responseBox)
		closeDialog()
		if (length(groups) == 0){
			errorCondition(recall=multiWayAnova, message=gettextRcmdr("You must select at least one factor."))
			return()
		}
		if (length(response) == 0){
			errorCondition(recall=multiWayAnova, message=gettextRcmdr("You must select a response variable."))
			return()
		}
		.activeDataSet <- ActiveDataSet()
		groups.list <- paste(paste(groups, "=", .activeDataSet, "$", groups, sep=""), collapse=", ")
		doItAndPrint(paste(modelValue, " <- (lm(", response, " ~ ", paste(groups, collapse="*"),
						", data=", .activeDataSet, "))", sep=""))
		doItAndPrint(paste("Anova(", modelValue, ")", sep=""))
		doItAndPrint(paste("tapply(", .activeDataSet, "$", response, ", list(", groups.list,
						"), mean, na.rm=TRUE) # means", sep=""))
		doItAndPrint(paste("tapply(", .activeDataSet, "$", response, ", list(", groups.list,
						"), sd, na.rm=TRUE) # std. deviations", sep=""))
		doItAndPrint(paste("tapply(", .activeDataSet, "$", response, ", list(", groups.list,
						"), function(x) sum(!is.na(x))) # counts", sep=""))
		activeModel(modelValue)
		pairwise <- tclvalue(pairwiseVariable)
		if (pairwise == 1) {
			doItAndPrint(paste("TukeyHSD(aov(", modelValue, "))", sep=""))
			doItAndPrint(paste("plot(TukeyHSD(aov(", modelValue, ")))", sep=""))
		}
		tkfocus(CommanderWindow())
	}
	OKCancelHelp(helpSubject="Anova", model=TRUE)
	tkgrid(labelRcmdr(modelFrame, text=gettextRcmdr("Enter name for model: ")), model, sticky="w")
	tkgrid(modelFrame, sticky="w", columnspan=2)
	tkgrid(getFrame(groupBox), getFrame(responseBox), sticky="nw")
	tkgrid(labelRcmdr(optionsFrame, text=gettextRcmdr("Tukey pairwise comparisons of means")), pairwiseCheckBox, sticky="w")
	tkgrid(optionsFrame, sticky="w", columnspan=2)
	tkgrid(buttonsFrame, columnspan=2, sticky="w")
	dialogSuffix(rows=4, columns=2)
}

